package com.rem.rabbitmq.boot.Dpriority;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 优先级队列
 *
 * @author Rem
 * @date 2021-12-29
 */

@Configuration
public class PriotiryConfig {

    public static final String PRIOTIRY_EXCHANGE = "PRIOTIRY_EXCHANGE";

    public static final String PRIOTIRY_ROUTING_KEY = "PRIOTIRY_ROUTING_KEY";

    public static final String PRIOTIRY_QUEUE = "PRIOTIRY_QUEUE";

    @Bean("priotiryExchange")
    public DirectExchange priotiryExchange() {
        return ExchangeBuilder.directExchange(PRIOTIRY_EXCHANGE).build();
    }

    /**
     * 设置优先级 0~255 官网推荐1-10 设置高比较吃内存和cpu
     * 发送的消息也必须设置优先级
     *
     * @return
     */
    @Bean("priotiryQueue")
    public Queue priotiryQueue() {
        return QueueBuilder.durable(PRIOTIRY_QUEUE).maxPriority(5).build();
    }

    @Bean
    public Binding priotiryQueueaBinding(@Qualifier("priotiryQueue") Queue priotiryQueue, @Qualifier("priotiryExchange") DirectExchange priotiryExchange) {
        return BindingBuilder.bind(priotiryQueue).to(priotiryExchange).with(PRIOTIRY_ROUTING_KEY);
    }


}
